
package simpleplaypen;

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author nomad
 */
public class playpen extends JFrame implements KeyListener, ActionListener
{
	public JPanel panel = new JPanel();
	public JPanel[] panels = new JPanel[4];
	public Color buttoncolor = null;
	public static Random rand = new Random(System.nanoTime());
	public static Color[] colors = {Color.red, Color.green, Color.blue, Color.CYAN, Color.MAGENTA, Color.orange, Color.PINK, Color.yellow};

	public static void main(String[] args)
	{
		playpen play = new playpen();

		try
		{
			play.initDisplay();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	public void initDisplay() throws Exception
	{
		String[] keylabel0 = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};
		String[] keylabel1 = {"Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"};
		String[] keylabel2 = {"A", "S", "D", "F", "G", "H", "J", "K", "L"};
		String[] keylabel3 = {"Z", "X", "C", "V", "B", "N", "M"};
		JButton button = null;

		this.addKeyListener(this);
		this.add(panel);
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

		// numbers
		panels[0] = new JPanel();
		panels[0].setLayout(new FlowLayout());
		panels[0].addKeyListener(this);
		for(String str : keylabel0)
		{
			button = new JButton(str);
			button.setPreferredSize(new Dimension(120, 120));
			button.addKeyListener(this);
			button.addActionListener(this);
			panels[0].add(button);
		}
		panel.add(panels[0]);

		// letters first row
		panels[1] = new JPanel();
		panels[1].setLayout(new FlowLayout());
		panels[1].addKeyListener(this);
		for(String str : keylabel1)
		{
			button = new JButton(str);
			button.setPreferredSize(new Dimension(120, 120));
			button.addKeyListener(this);
			button.addActionListener(this);
			panels[1].add(button);
		}
		panel.add(panels[1]);

		// letters second row
		panels[2] = new JPanel();
		panels[2].setLayout(new FlowLayout());
		panels[2].addKeyListener(this);
		for(String str : keylabel2)
		{
			button = new JButton(str);
			button.setPreferredSize(new Dimension(120, 120));
			button.addKeyListener(this);
			button.addActionListener(this);
			panels[2].add(button);
		}
		panel.add(panels[2]);

		// letters third row
		panels[3] = new JPanel();
		panels[3].setLayout(new FlowLayout());
		panels[3].addKeyListener(this);
		for(String str : keylabel3)
		{
			button = new JButton(str);
			button.setPreferredSize(new Dimension(120, 120));
			button.addKeyListener(this);
			button.addActionListener(this);
			panels[3].add(button);
		}
		panel.add(panels[3]);

		buttoncolor = panels[0].getComponent(0).getBackground();

		setUndecorated(true);
		setFocusable(true);
		setResizable(false);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		validate();

		GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();

		if(!gd.isFullScreenSupported())
			throw new Exception("FUll Screen not supported");

		gd.setFullScreenWindow(this);

	}

	protected Component getComponent(char c)
	{
		Component ret = null;

		switch(c)
		{
			case '1':
				ret = panels[0].getComponent(0);
				break;
			case '2':
				ret = panels[0].getComponent(1);
				break;
			case '3':
				ret = panels[0].getComponent(2);
				break;
			case '4':
				ret = panels[0].getComponent(3);
				break;
			case '5':
				ret = panels[0].getComponent(4);
				break;
			case '6':
				ret = panels[0].getComponent(5);
				break;
			case '7':
				ret = panels[0].getComponent(6);
				break;
			case '8':
				ret = panels[0].getComponent(7);
				break;
			case '9':
				ret = panels[0].getComponent(8);
				break;
			case '0':
				ret = panels[0].getComponent(9);
				break;

			case 'q':
				ret = panels[1].getComponent(0);
				break;
			case 'w':
				ret = panels[1].getComponent(1);
				break;
			case 'e':
				ret = panels[1].getComponent(2);
				break;
			case 'r':
				ret = panels[1].getComponent(3);
				break;
			case 't':
				ret = panels[1].getComponent(4);
				break;
			case 'y':
				ret = panels[1].getComponent(5);
				break;
			case 'u':
				ret = panels[1].getComponent(6);
				break;
			case 'i':
				ret = panels[1].getComponent(7);
				break;
			case 'o':
				ret = panels[1].getComponent(8);
				break;
			case 'p':
				ret = panels[1].getComponent(9);
				break;

			case 'a':
				ret = panels[2].getComponent(0);
				break;
			case 's':
				ret = panels[2].getComponent(1);
				break;
			case 'd':
				ret = panels[2].getComponent(2);
				break;
			case 'f':
				ret = panels[2].getComponent(3);
				break;
			case 'g':
				ret = panels[2].getComponent(4);
				break;
			case 'h':
				ret = panels[2].getComponent(5);
				break;
			case 'j':
				ret = panels[2].getComponent(6);
				break;
			case 'k':
				ret = panels[2].getComponent(7);
				break;
			case 'l':
				ret = panels[2].getComponent(8);
				break;

			case 'z':
				ret = panels[3].getComponent(0);
				break;
			case 'x':
				ret = panels[3].getComponent(1);
				break;
			case 'c':
				ret = panels[3].getComponent(2);
				break;
			case 'v':
				ret = panels[3].getComponent(3);
				break;
			case 'b':
				ret = panels[3].getComponent(4);
				break;
			case 'n':
				ret = panels[3].getComponent(5);
				break;
			case 'm':
				ret = panels[3].getComponent(6);
				break;

			default:
				ret = panels[rand.nextInt(4)];
			}

		return ret;
	}
	
	public void keyTyped(KeyEvent e)
	{
//		throw new UnsupportedOperationException("Not supported yet.");
	}

	public void keyPressed(KeyEvent e)
	{
		Component	block = getComponent(e.getKeyChar());

		if(block != null)
			block.setBackground(colors[rand.nextInt(colors.length)]);

//		System.out.println("KeyPressed: " + e.getKeyChar());
	}

	public void keyReleased(KeyEvent e)
	{
		Component	block = getComponent(e.getKeyChar());

		if(block != null)
			block.setBackground(buttoncolor);

//		System.out.println("KeyReleased: " + e.getKeyChar());

	}

	public void actionPerformed(ActionEvent e)
	{
		JButton button = (JButton)e.getSource();

		button.setBackground(colors[rand.nextInt(colors.length)]);
	}

}